package com.tico.translatereditor.domain;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Comment;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.json.Json;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;

import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

@Data
@Table("t_message_models")
@Comment("模版设定")
/**
 * @Description
 * @Author maodajun@gmail.com
 * @Date 2020/9/14
 */
public class MessageModel {

    @Column("content")
    @Comment("模版")
    private String content;

    @Column("json")
    @Comment("测试数据")
    private String json;

    public String render() {
        MustacheFactory mf = new DefaultMustacheFactory();
        if(Strings.isBlank(content)){
            return "";
        }
        if(Strings.isBlank(json)){
            return "";
        }

        Mustache mustache = mf.compile(new StringReader(getContent()), "example");
        Writer writer = new StringWriter();
        mustache.execute(writer, Json.fromJson(getJson()));


        return writer.toString();
    }

    public static String render(String content,String json){

        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile(new StringReader(content), "example");
        Writer writer = new StringWriter();
        mustache.execute(writer, Json.fromJson(json));
        return writer.toString();
    }
}
